package com.gamota.mobi;

import java.util.Locale;

import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;
import com.gamota.mobi.fragment.DownloadLogFragment;
import com.gamota.mobi.util.Constants;
import com.google.analytics.tracking.android.EasyTracker;

public class DownloadLogActivity extends SherlockFragmentActivity {

	private ViewPager viewPager;
	private LogFragmentAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setTheme(R.style.Theme_Styled_Home);
		setThemeLowEndDevice(R.drawable.action_bar_home);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_download_log);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle(R.string.download_log);
		viewPager = (ViewPager) findViewById(R.id.pager);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int pageMargin = metrics.widthPixels / 48;
        
		adapter = new LogFragmentAdapter(getSupportFragmentManager());
		viewPager.setOffscreenPageLimit(6);
		viewPager.setPageMargin(pageMargin);
		viewPager.setPageMarginDrawable(R.drawable.bg_pagemargin_xml);
		viewPager.setAdapter(adapter);
		PagerSlidingTabStrip indicator = (PagerSlidingTabStrip) findViewById(R.id.indicator);
        indicator.setViewPager(viewPager);
        indicator.setIndicatorColorResource(R.color.app_indicator);
        int pixelSize = (int) (14 * metrics.scaledDensity); 
        indicator.setTextSize(pixelSize);
	}
	
	private void setThemeLowEndDevice(int drawableId){
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(drawableId);
            bg.setTileModeX(TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);
        }
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private class LogFragmentAdapter extends FragmentStatePagerAdapter {

		protected String[] TITLE = new String[] { getString(R.string.store_app).toUpperCase(Locale.US), getString(R.string.store_ebook).toUpperCase(Locale.US), getString(R.string.store_comic).toUpperCase(Locale.US), getString(R.string.store_film).toUpperCase(Locale.US)};

		public LogFragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			DownloadLogFragment frag = new DownloadLogFragment();
			Bundle bun = new Bundle();
			if(position == 0){
				bun.putInt(Constants.STORE_KEY, 2);
			} else if(position == 1){
				bun.putInt(Constants.STORE_KEY, 4);
			} else if(position == 2){
				bun.putInt(Constants.STORE_KEY, 5);
			} else {
				bun.putInt(Constants.STORE_KEY, 6);
			}
			frag.setArguments(bun);
			return frag;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return TITLE.length;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return TITLE[position];
		}
		
		@Override
		public void setPrimaryItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			if(position > 0){
				DownloadLogFragment frag = (DownloadLogFragment) object;
				frag.init();
			}
		}
	}
}
